package com.situ.web.common;

import java.io.Serializable;

/**
 * 通用响应对象，ajax请求返回的数据都封装成ServerResponse
 * 类比：PageBean
 */
public class ServerResponse<T> implements Serializable {
	private static final int ERROR = 0;
	private static final int SUCCESS = 1;
	private static final int NEED_LOGIN = 2;
	private static final int ILLEGAL_PERMISSION = 3;
	
	//当前状态（程序员判断状态）:成功、失败、未登录、没有权限
	private Integer code;
	//描述信息（主要是给用户看的提示信息）
	private String msg;
	//总数量（按理说是不需要，主要是给LayUI table）
	private Integer count;
	//后台返回给前端的数据 Object
	private T data; // List<Student>, int, String, true

	private ServerResponse() {
	}
	
	private ServerResponse(Integer code) {
		this.code = code;
	}

	private ServerResponse(Integer code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	private ServerResponse(Integer code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	private ServerResponse(Integer code, String msg, Integer count, T data) {
		this.code = code;
		this.msg = msg;
		this.count = count;
		this.data = data;
	}

	//只是告诉前台：成功这种状态
	public static<T> ServerResponse<T> createSuccess() {
		return new ServerResponse<>(SUCCESS);
	}

	//告诉前台：code,msg
	public static <T> ServerResponse<T> createSuccess(String msg) {
		return new ServerResponse<>(SUCCESS, msg);
	}

	//告诉前台：code,msg,data
	public static <T> ServerResponse<T> createSuccess(String msg, T data) {
		return new ServerResponse<>(SUCCESS, msg, data);
	}

	//告诉前台：code,msg,count,data
	public static <T> ServerResponse<T> createSuccess(String msg, Integer count, T data) {
		return new ServerResponse<>(SUCCESS, msg, count, data);
	}

	//只是告诉前台：失败这种状态
	public static <T> ServerResponse<T> createError() {
		return new ServerResponse<>(ERROR);
	}

	//告诉前台：code,msg
	public static <T> ServerResponse<T> createError(String msg) {
		return new ServerResponse<>(ERROR, msg);
	}

	//告诉前台：code,msg,data
	public static <T> ServerResponse<T> createError(String msg, T data) {
		return new ServerResponse<>(ERROR, msg, data);
	}

	public boolean isSuccess() {
		return code == SUCCESS;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public Integer getCount() {
		return count;
	}

	public void setCount(Integer count) {
		this.count = count;
	}

	public static void main(String[] args) {
		ServerResponse serverResponse2 = new ServerResponse();
		serverResponse2.setCode(SUCCESS);
		serverResponse2.setMsg("成功");
		
		ServerResponse serverResponse1 = ServerResponse.createSuccess();
		ServerResponse server = ServerResponse.createSuccess("删除成功");
	}
	
	
}
